/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fiap.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author pf0591
 */
@WebServlet(urlPatterns="/logar")
public class Login extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        String pwd = req.getParameter("pwd");
        String user = req.getParameter("user");
        
        boolean autenticado = autenticar(user, pwd);
        if( autenticado ){           
           HttpSession session =  req.getSession();
           session.setAttribute("logado", true);
           //AUTENTICOU, REDIRECIONAR PARA O MENU
           req.getRequestDispatcher("index.html").forward(req, resp);
        }else{
           //NÃO AUTENTICOU, REDIRECIONAR PARA O LOGIN
           req.getRequestDispatcher("login.html").forward(req, resp); 
           
        }
    }
    
    //SUBSTITUIR POR ACESSO JPA AO BANCO
    private boolean autenticar(String user,String pwd){
        //JPA vai na base
        if(pwd != null && pwd.equals("12345")){
            return true;
            
            
            
        } else {
            return false;
            
            
            
        }
        
    }
    
}
